package com.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.entity.Visit;
import com.mapper.DailyDrugexpenseMapper;
import com.mapper.PrescriptionDetailMapper;
import com.mapper.PrescriptionMapper;
import com.mapper.UseDrugMapper;
import com.mapper.VisitMapper;
import com.service.VisitService;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.ArrayList;

@Service
public class VisitServiceImpl extends ServiceImpl<VisitMapper, Visit> implements VisitService {
    @Autowired
    VisitMapper visitMapper;
    @Autowired
    PrescriptionMapper prescriptionMapper;
    @Autowired
    PrescriptionDetailMapper prescriptionDetailMapper;
    @Autowired
    UseDrugMapper useDrugMapper;
    @Autowired
    DailyDrugexpenseMapper dailyDrugexpenseMapper;

    @Override
    public List<Visit> findAll() {
        return visitMapper.selectList(null);
    }

    public static Date convertStringToSqlDate(String dateString) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            java.util.Date date = dateFormat.parse(dateString);
            return new java.sql.Date(date.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void updateVisitById(int visitId, String diagnosisName, String hospitalDepartment, String describtion,
            int bedNumber, String state, String start, String end, Map<String, Integer> drugList) {
        /*
         * visitId : visit表的visit_id
         * diagnosisName : presrciption表的diagnosis_name
         * hospitalDepartment : visit表的hospital_department
         * describtion : visit表的describtion
         * bedNumber : visit表的bed_number
         * state : visit表的state
         * start : prescriptiondetail表的start_date
         * end : prescriptiondetail表的end_date
         * drugList : List列表
         * DrugDetails : drugId, quantity, price, startDate, endDate
         * drugId : usedrug表的drug_id
         * quatity : usedrug表的quantity
         * price : usedrug表的daily_price
         * startDate : usedrug表的start_date
         * endDate : usedrug表的end_date
         */
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); 
        visitMapper.updateById(visitId, hospitalDepartment, describtion, bedNumber, state);
        System.out.println("visit跑完啦！");
        prescriptionMapper.insert(visitId, diagnosisName);
        System.out.println("prescription跑完啦！");
        int prescriptionId = selectByVisitId(visitId);
        prescriptionDetailMapper.insert(prescriptionId, convertStringToSqlDate(start), convertStringToSqlDate(end));
        System.out.println("prescriptionDetail跑完啦！" + prescriptionId);
        int prescriptionDetailId = prescriptionDetailMapper.selectByPrescriptionId(prescriptionId);
        System.out.println("prescriptionDetailId ： " + prescriptionDetailId);
        addAllDrug(prescriptionDetailId, drugList);
        System.out.println("userDrug跑完了");
        List<LocalDate> dateList = getDateRange(LocalDate.parse(start, formatter),LocalDate.parse(end, formatter));
        for (LocalDate localDate : dateList) {
            int dailyCost = useDrugMapper.getDailyPriceByPrescriptionId(prescriptionDetailId);
            dailyDrugexpenseMapper.insert(prescriptionDetailId, visitId,java.sql.Date.valueOf(localDate),dailyCost);
        }
    }

    @Override
    public int selectByVisitId(int visitId) {
        return prescriptionMapper.selectByVisitId(visitId);
    }

    private void addAllDrug(int prescriptionDetailId, Map<String, Integer> drugList) {
        for (String key : drugList.keySet()) {
            int drugId = useDrugMapper.selectByName(key);
            useDrugMapper.insert(prescriptionDetailId, drugId, drugList.get(key));
        }
    }

    List<LocalDate> getDateRange(LocalDate startDate, LocalDate endDate) {
        List<LocalDate> dateList = new ArrayList<>();
        LocalDate currentDate = startDate;

        while (currentDate.isBefore(endDate) || currentDate.isEqual(endDate)) {
            dateList.add(currentDate);
            currentDate = currentDate.plusDays(1);
        }

        return dateList;
    }

    @Override
    public List<Visit> selectUntreated(Visit visit) {
        return visitMapper.selectUntreated(visit.getState());
    }
}
